﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using YFIN.Service.NewsAndCourse;
using System.Text.RegularExpressions;
using System.IO;

namespace YFIN.Service.Crawlers
{
    public class AnalyzeHtml
    {
        public IList<News> UrlList = new List<News>();

        public IList<News> GetUrlListByparseString(ref IList<News> list, News news, string InputString)
        {
            list = GetUrlList(RegExFindHref, news, InputString);
            return list;
        }

        public IList<News> GetUrlListByparseString(News news, string InputString)
        {
            return GetUrlListByparseString(ref UrlList, news, InputString);
        }

        private static string RegExprHREF = @"(?<=a\s+([^>]+\s+)?href\=[\x27\x22])(?<1>[^\x27\x22]*)(?=[\x27\x22])";
        private static Regex RegExFindHref = new Regex(RegExprHREF, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private int i = 0;
        public IList<News> list = new List<News>();
        private IList<News> GetUrlList(Regex regExpr, News father_url, string html)
        {

            for (Match m = regExpr.Match(html); m.Success; m = m.NextMatch())
            {
                string href = m.Groups[1].ToString();

                // filter non-real relation urls:
                if (String.IsNullOrEmpty(href) ||
                    href.StartsWith("#") ||
                    href.StartsWith("mailto:") ||
                    href.StartsWith("javascript:"))
                {
                    continue;
                }

                href = System.Web.HttpUtility.HtmlDecode(href);

                News news = null;
                try
                {
                    news = ConvertToAbsoluteUrl(href, father_url);
                    if (news != null)
                    {


                        string dd = news.UrlLink.ToString();
                        //string conturl1 =ConfigurationManager.AppSettings["ContUrl"];
                        int index = dd.IndexOf("http://www.pbc.gov.cn/publish/goutongjiaoliu/524/2013/");
                        string aa = index == -1 ? string.Empty : dd.Substring(index);
                        if (aa == String.Empty)
                        {
                            continue;
                        }
                        string Board = GetResponse(news.UrlLink.ToString());
                        Regex RegExFindTime = new Regex(@"(\d{4}-\d{2}-\d{2})");

                        string date = null;
                        string title = null;
                        for (Match m1 = RegExFindTime.Match(Board); m1.Success; m1 = m1.NextMatch())
                        {

                            date += m1.Groups[1].ToString();
                        }
                        //Regex RegexfindTitle = new Regex(@"(?s)((?i)<title>([^<]*)(?=</title>)");
                        Regex RegexfindTitle = new Regex(@"(?s)<title>(.*?)</title>");
                        for (Match m2 = RegexfindTitle.Match(Board); m2.Success; m2 = m2.NextMatch())
                        {

                            title += m2.Groups[1].ToString();
                        }
                        news.Type = 0;
                        news.Title = title;
                        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
                        DateTime Date = DateTime.Parse(date, culture);
                        news.Time = Date;
                        news.Url = news.UrlLink.ToString();
                        new NewsService().Add(news);
                      
                       
                    }

                }
                catch (Exception)
                {
                }

            }
            return list;

        }

        public String GetResponse(string news)
        {
            try
            {
                WebRequest requset = WebRequest.Create(news);
                WebResponse response = requset.GetResponse();

                Stream s = response.GetResponseStream();
                StreamReader sr = new StreamReader(s, System.Text.Encoding.UTF8);
                string html = sr.ReadToEnd();
                s.Close();
                sr.Close();
                return html;
            }
            catch (Exception)
            {
                return String.Empty;
            }

        }

        private News ConvertToAbsoluteUrl(string news, News father_url)
        {
            // we try to prevent the exception caused in the case the news is relative
            // (no scheme info) just for speed
            if (news.IndexOf(Uri.SchemeDelimiter) < 0 && father_url != null)
            {
                try
                {
                    string link = father_url.UrlLink.AbsoluteUri;
                    return new News(father_url, father_url.CurrentDepth + 1, new Uri(new Uri(link), news));
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                try
                {
                    if (news.IndexOf(Uri.SchemeDelimiter) > 0)
                    {
                        if (father_url != null)
                        {
                            string link = father_url.UrlLink.AbsoluteUri;
                            News newurl = new News(father_url, father_url.CurrentDepth + 1, new Uri(new Uri(link), news));
                            if (newurl.UrlLink.Host == father_url.UrlLink.Host)
                            {
                                return newurl;
                            }
                            else
                            {
                                return new News(new Uri(new Uri(link), news));
                            }
                        }
                        else
                        {
                            string link = father_url.UrlLink.AbsoluteUri;
                            return new News(new Uri(new Uri(link), news));
                        }
                    }
                }
                catch (Exception)
                {
                    return null;
                }
                return null;
            }
        }
    }
}